package fr.nts.framework.front.form.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.nts.framework.core.commun.tool.ApplicationContextHelper;
import fr.nts.framework.front.form.AbstractForm;
import fr.nts.framework.front.form.html.HtmlFormCreator;
import fr.nts.framework.front.form.manager.FormManager;

/**
 * Tag permettant la génération d'un formulaire via l'api NTS
 * 
 * @author sco
 * @version 1.0.0
 */
public class SimpleFormTag extends BodyTagSupport{

	private static final long serialVersionUID = 1L;

	protected static final Logger log = LoggerFactory.getLogger(SimpleFormTag.class);
	
	private final static String HTML_FORM_CREATOR_BEAN = "htmlFormCreator";
	
	private final static String FORM_MANAGER = "formManager";
	private final static String DEFAULT_CSS_CLASS = "simple-form";
	
	/**
	 * Le formulaire associé au tag
	 */
	private AbstractForm form;
	
	/**
	 * L'action de soumission du formulaire
	 */
	private String action;
	
	/**
	 * La méthode de soumission
	 */
	private String method;

    /**
     * Le titre
     */
    private String title;
    
	/**
	 * La class css du formulaire
	 */
    private String formClass = DEFAULT_CSS_CLASS;
    
    /**
     * Le message bundle
     */
    private String bundle;
    
    
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
    public int doStartTag() throws JspException{
    	log.debug("On va générer un formulaire HTML NTS à partir de l'objet {}", form);
    	
    	// Formulaire non valide
    	if(!getFormManager().isAValidForm(form)){
    		return SKIP_BODY;
    	}

    	String tag = generateFormTag();
    	
    	try{
			pageContext.getOut().print(tag);
		}catch(IOException e){
			log.warn("Problème génération du tag FormTag.", e);
		}
    	
    	return SKIP_BODY;
    }
    
    
    /**
     * Génère le tag <form...> </form>
     */
    private String generateFormTag(){
    	return getHtmlFormCreator().generateForm(form, 
									             formClass, 
									             title, 
									             method, 
									             action, 
									             bundle);
    }
    

    
    /**
     * Retourne le {@link FormManager}
     */
    private FormManager getFormManager(){
    	return (FormManager)ApplicationContextHelper.getBean(FORM_MANAGER);
    }
    
    
    /**
     * Le generateur de code html
     */
    private HtmlFormCreator getHtmlFormCreator(){
    	return (HtmlFormCreator)ApplicationContextHelper.getBean(HTML_FORM_CREATOR_BEAN);
    }
    
    
	// GETTER / SETTER
	public AbstractForm getForm(){
		return form;
	}

	public void setForm(AbstractForm form){
		this.form = form;
	}

	public String getAction(){
		return action;
	}

	public void setAction(String action){
		this.action = action;
	}

	public String getMethod(){
		return method;
	}

	public void setMethod(String method){
		this.method = method;
	}

	public String getFormClass(){
		return formClass;
	}

	public void setFormClass(String formClass){
		this.formClass = formClass;
	}

	public String getBundle(){
		return bundle;
	}

	public void setBundle(String bundle){
		this.bundle = bundle;
	}

	public String getTitle(){
		return title;
	}

	public void setTitle(String title){
		this.title = title;
	}
}